package common.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 导入测试数据，是否扣费 汉字翻译对应code
 *
 * @author yuanqinglong
 * @since 2021/3/8 19:02
 */
public class ImportIsCostConverter implements Converter<String> {

    /**
     * NOT_IGNORE：不忽略、 NOT_COST：不扣费
     */
    public static final String NOT_IGNORE = "00", NOT_COST = "0";
    /**
     * IGNORE：忽略、IS_COST：是扣费
     */
    public static final String IGNORE = "01", IS_COST = "1";

    /**
     * 将excel中是否扣费的汉字（是、否）翻译对应code
     */
    public static final Map<String, String> COST_CHINESE_TO_CODE = new HashMap<>();
    public static final Map<String, String> COST_CODE_TO_CHINESE = new HashMap<>();

    static {
        COST_CHINESE_TO_CODE.put("是", IS_COST);
        COST_CHINESE_TO_CODE.put("否", NOT_COST);
        COST_CODE_TO_CHINESE.put("1", "是");
        COST_CODE_TO_CHINESE.put("0", "否");
    }


    @Override
    public Class<?> supportJavaTypeKey() {
        return LocalDateTime.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public String convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return COST_CODE_TO_CHINESE.getOrDefault(cellData.getStringValue(), cellData.getStringValue());
    }


    @Override
    public CellData<String> convertToExcelData(String cost, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return new CellData<>(cost);
    }


}
